# -*- coding: utf-8 -*-
import socket
import time
import tools
from bdtime import tt


CODING_TYPE = tools.get_coding_type()


# client 客户端
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_address = ("", tools.get_a_not_in_use_port())
client_socket.bind(client_address)

# server 服务端

SERVER_PORT = 8888
# SERVER_IP = tools.get_host_ip()
SERVER_IP = "127.0.0.1"
server_address = (SERVER_IP, SERVER_PORT)  # 接收方 服务器的ip地址和端口号

# server_address = ('10.121.2.2', 8000)
print(f"--- 本客户端[{client_address}] -> 服务端[{server_address}] ---\n")
dir(client_socket)
type(client_socket)


while True:
    tt.sleep(1)

    if tt.is_pressed('alt + s'):
        break

    print('--- tt.now ', tt.now(1))

    start = time.time()  # 获取当前时间

    # msg = input(f"--- 本客户端[{client_socket.getsockname()}] -> 服务端[{server_address}] ---\n请输入要发送的内容：")
    msg = f'client: {tt.now()}' + '123456789' * 100
    msg = msg.encode(CODING_TYPE)

    client_socket.sendto(msg, server_address)   # 将msg内容发送给指定接收方

    _receive_data, addr = client_socket.recvfrom(1024)
    receive_data = tools.decode(_receive_data)
    print(f'--- tt.now receive_data [{receive_data}] from_addr: [{addr}]')      # 打印接收的内容

    # if receive_data == 'Close':
    #     print(f'*** Close|{tt.now()}| 收到Close消息! 程序关闭!')
    #     break
